package router

import (
	"crow-k8s/src/config"
	"crow-k8s/src/service"
	"github.com/gin-gonic/gin"
)

// GetNamespaceList 查看namespace列表
// @Summary 查看namespace列表
// @Description 查看namespace列表
// @Tags Namespace
// @Success 200 {object} response.Response{data=v1.NamespaceList} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/namespace/list [get]
// @Security Bearer
func GetNamespaceList(c *gin.Context) {
	namespaceList, err := service.GetNamespaceList(config.K8sClientSet)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, namespaceList)
}

// GetNamespace 查看指定namespace
// @Summary 查看查看指定namespace
// @Description 查看查看指定namespace
// @Tags Namespace
// @Param namespace_name query string true "namespace名字"
// @Success 200 {object} response.Response{data=v1.Namespace} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/namespace [get]
// @Security Bearer
func GetNamespace(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	namespaceInfo, err := service.GetNamespace(config.K8sClientSet, namespaceName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, namespaceInfo)
}

// CreateNamespace 创建namespace
// @Summary 创建namespace
// @Description 创建namespace
// @Tags Namespace
// @Accept  application/json
// @Product application/json
// @Param data body service.CreateNamespaceRequest true "创建namespace的信息"
// @Success 200 {object} response.Response{data=v1.Namespace} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/namespace [post]
// @Security Bearer
func CreateNamespace(c *gin.Context) {
	var request service.CreateNamespaceRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	namespaceInfo, err := service.CreateNamespace(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, namespaceInfo)
}

// DeleteNamespace 删除namespace
// @Summary 删除namespace
// @Description 删除namespace
// @Tags Namespace
// @Param namespace_name path string true "namespace名字"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/namespace/{namespace_name} [delete]
// @Security Bearer
func DeleteNamespace(c *gin.Context) {
	name := c.Param("namespace_name")
	err := service.DeleteNamespace(config.K8sClientSet, name)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "删除成功")
}
